﻿/***************************************************************************
 *		ElGee Google Chart API Helper
 *		Copyright(C) 2011 by Laurent Geffroy lgeffroy@gmail.com
 * 
 *		This program is free software; you can redistribute it and/or 
 *		modify it under the terms of the GNU General Public License as
 *		published by the Free Software Foundation; either version 2 of 
 *		the License, or (at your option) any later version.
 *		
 *		This program is distributed WITHOUT ANY WARRANTY; without even the
 *		implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 *		See the GNU General Public License for more details.
 *	
 *      For particular licensing, please contact me at lgeffroy@gmail.com
 *****************************************************************************/

using System;
using System.Globalization;
using System.Text;

namespace ElGee.Google.Charts
{
    /// <summary>
    /// Style for the Pie Chart
    /// </summary>
    public class PieChartStyle : ChartStyle
    {
        private readonly string[] _pieChartcolorExclusion = { "#cccccc", "#ccc" };

        /// <summary>
        /// Gets or sets a value indicating whetheris in 3D.
        /// If set to true, displays a three-dimensional chart.
        /// </summary>
        /// <value><c>true</c> if [is3 D]; otherwise, <c>false</c>.</value>
        public bool is3D {get; set;}

        /// <summary>
        /// Gets or sets the type of the pie slice text.
        /// The content of the text displayed on the slice. Can be one of the following:
        /// 'percentage' - The percentage of the slice size out of the total.
        /// 'value' - The quantitative value of the slice.
        /// 'label' - The name of the slice.
        /// 'none' - No text is displayed.
        /// </summary>
        /// <value>The type of the pie slice text.</value>
        public TypeOfPieSliceText PieSliceTextType { get; set; }

        /// <summary>
        /// Gets or sets the pie slice text style.
        /// An object that specifies the slice text style. The object has this format:
        /// {color: <string>, fontName: <string>, fontSize: <number>}
        /// The color can be any HTML color string, for example: 'red' or '#00cc00'. Also see fontName and fontSize.
        /// </summary>
        /// <value>The pie slice text style.</value>
        public FontWithColor PieSliceTextStyle { get; set; }

        /// <summary>
        /// Gets or sets the slice visibility threshold.
        /// The slice relative part, below which a slice will not show individually. All slices that have not passed this threshold will be combined to a single slice, whose size is the sum of all their sizes. Default is not to show individually any slice which is smaller than half a degree.
        /// </summary>
        /// <value>The slice visibility threshold.</value>
        public UInt16 SliceVisibilityThreshold { get; set; }

        /// <summary>
        /// Gets or sets the color of the pie residue slice.
        /// Color for the combination slice that holds all slices below sliceVisibilityThreshold.
        /// </summary>
        /// <value>The color of the pie residue slice.</value>
        public string PieResidueSliceColor { get; set; }

        /// <summary>
        /// Gets or sets the pie residue slice label.
        /// A label for the combination slice that holds all slices below sliceVisibilityThreshold.
        /// </summary>
        /// <value>The pie residue slice label.</value>
        public string PieResidueSliceLabel { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="PieChartStyle"/> class.
        /// </summary>
        public PieChartStyle() : base()
        {
            PieSliceTextType = TypeOfPieSliceText.Percentage;
            PieSliceTextStyle = new FontWithColor();
            SliceVisibilityThreshold = 720;
            PieResidueSliceColor = "#ccc";
            PieResidueSliceLabel = "Other";
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            string baseParams = base.ToString();
            if (!String.IsNullOrWhiteSpace(baseParams))
                sb.Append(baseParams).Append(",");
            
            // PieSliceText
            if ((PieSliceTextType != TypeOfPieSliceText.Percentage) && (PieSliceTextType != TypeOfPieSliceText.Undefined))
                sb.Append("pieSliceText: '").Append(PieSliceTextType.ToString().ToLower()).Append("',");

            // PieSliceTextStyle
            if (PieSliceTextStyle != null)
            {
                if (!PieSliceTextStyle.IsEmpty)
                    sb.Append("pieSliceTextStyle: {").Append(PieSliceTextStyle.ToString()).Append("},");
            }

            // SliceVisibilityThreshold
            if (SliceVisibilityThreshold != 720)
                sb.Append("sliceVisibilityThreshold: 1/").Append(SliceVisibilityThreshold.ToString(CultureInfo.InvariantCulture)).Append(",");

            // PieResidueSliceColor
            if ((!String.IsNullOrWhiteSpace(PieResidueSliceColor)) && (Array.IndexOf(_pieChartcolorExclusion, PieResidueSliceColor.ToLower()) < 0))
                sb.Append("pieResidueSliceColor: '").Append(SecureString(PieResidueSliceColor)).Append("',");

            // PieResidueSliceLabel
            if (PieResidueSliceLabel.ToLower() != "other")
                sb.Append("pieResidueSliceLabel: '").Append(SecureString(PieResidueSliceLabel)).Append("',");

            // Is3D
            if (is3D)
                sb.Append("is3D: true,");

            // Removing the last comma
            sb.Remove(sb.Length - 1, 1);

            return sb.ToString();
        }
    }
}
